package com.guigu.gulicommon.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import com.guigu.gulicommon.config.threadPool.ThreadPoolConfig;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 该类用于配置各位服务公共的配置类
 * <p>
 * 各个服务通过引入本模块,然后再在springboot启动类上加入@Import(CommonConfiguration.class)
 * 可以导入该类, 从而实现统一生效公共配置类
 *
 * @author 陈俊杰
 */
@Configuration
@Log4j2
@Import({RedisConfig.class, ThreadPoolConfig.class, FeignConfig.class, RabbitMqConfig.class, LoadBalancedRuleConfig.class})
public class CommonConfiguration {
    /**
     * 全局异常处理
     *
     * @return ExceptionHandlerConfig 全局异常处理器
     */
    @Bean
    public ExceptionHandlerConfig exceptionHandlerConfig() {
        log.info("全局异常处理配置注册中....");
        log.info("全局异常处理配置注册成功....");
        return new ExceptionHandlerConfig();
    }

    /**
     * 配置jackson序列化
     *
     * @return
     */
    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        log.info("jackson序列器注册中....");
        JavaTimeModule timeModule = new JavaTimeModule();

        timeModule.addDeserializer(LocalDate.class,
                new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

        timeModule.addDeserializer(LocalDateTime.class,
                new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        timeModule.addSerializer(LocalDate.class,
                new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

        timeModule.addSerializer(LocalDateTime.class,
                new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
//        builder.featuresToEnable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);//允许反序列化时，遇到未知枚举值时，返回null
        log.info("jackson序列器注册成功....");
        return new ObjectMapper()
                //设置默认的序列化规则，遇到无法识别的枚举值时，返回null
                .enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
                //禁用
                //.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                .registerModule(timeModule)
                .registerModule(new ParameterNamesModule())
                .registerModule(new Jdk8Module());
    }


}
